<!DOCTYPE html>
<html lang="en">
<head>
    <title>插件管理</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='/admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="plugin-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">插件名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="plugin_name" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="plugin-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
{# 用户表格 #}
<div class="user-plugin user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="plugin-table" lay-filter="plugin-table"></table>
        </div>
    </div>
</div>
</body>
{# 表格操作 #}
<script type="text/html" id="plugin-toolbar">
    {% if authorize("admin:plugin:enable") %}
{#        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">#}
{#            <i class="pear-icon pear-icon-add"></i>#}
{#            新增#}
{#        </button>#}
    {% endif %}
    {% if authorize("admin:plugin:remove") %}
{#        <button class="pear-btn pear-btn-md" lay-event="batchRemove">#}
{#            <i class="pear-icon pear-icon-ashbin"></i>#}
{#            删除#}
{#        </button>#}
    {% endif %}
</script>

{# 用户修改操作 #}
<script type="text/html" id="plugin-bar">
    {% if authorize("admin:plugin:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="pear-icon pear-icon-ashbin"></i>
        </button>
    {% endif %}
</script>

{# 启动与禁用 #}
<script type="text/html" id="plugin-enable">
    <input type="checkbox" name="enable" value="{{ "{{ d.plugin_folder_name }}" }}" lay-skin="switch"
           lay-text="启用|禁用"
           lay-filter="plugin-enable"
            {{ "{{# if(d.enable==1){ }} checked {{# } }}" }} />
</script>

{# 用户注册时间 #}
<script type="text/html" id="plugin-createTime">
    {{ '  {{layui.util.toDateString(d.create_at,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>

{% include 'admin/common/footer.html' %}

<script>
    // 重启程序
    function restart() {
        layui.use(['layer'], function () {
                var $ = layui.jquery;
                let popup = layui.popup;
                layer.confirm('需要重启程序吗？这样可以使插件更改生效。（如果没有守护进程需要手动启动。）', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index)
                    let loading = layer.load()
                    $.ajax({
                        url: '/admin/monitor/kill',
                        dataType: 'json',
                        type: 'get',
                        error: function (result) {
                            setInterval(function () {
                                tip = false
                                $.ajax({
                                    url: '/admin/monitor',
                                    type: 'get',
                                    success: function (result) {
                                        layer.close(loading)
                                        if (tip == false) {
                                            popup.success("重启程序成功！", function () {
                                                window.location.reload()
                                            })
                                        }
                                    }
                                })
                            }, 1000);
                        }
                    })
                })
            }
        )
    }

    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree
        let popup = layui.popup
        let common = layui.common
        let MODULE_PATH = '/plugin/'

        // 表格数据
        let cols = [
            [
                {% if authorize("admin:plugin:remove") %}
                    {type: 'checkbox'},
                {% endif %}
                {field: 'plugin_name', minWidth: 100, title: '插件名称'},
                {field: 'plugin_version', title: '插件版本'},
                {field: 'plugin_description', title: '插件描述'},
                {field: 'plugin_folder_name', hide: true},
                {field: 'enable', title: '状态', templet: '#plugin-enable', width: 100, align: 'center'},
                {title: '操作', templet: '#plugin-bar', width: 120, align: 'center'}
            ]
        ]

        // 渲染表格数据
        table.render({
            elem: '#plugin-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#plugin-toolbar', /*工具栏*/
            text: {none: '暂无人员信息'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'] /*默认工具栏*/
        })


        // 禁用启用
        form.on('switch(plugin-enable)', function (obj) {
            let operate
            if (obj.elem.checked) {
                operate = 'enable'
            } else {
                operate = 'disable'
            }
            let loading = layer.load()
            $.ajax({
                url: MODULE_PATH + operate,
                data: JSON.stringify({plugin_folder_name: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading)
                    if (result.success) {
                        popup.success(result.msg)
                        restart()
                    } else {
                        popup.failure(result.msg)
                    }

                }
            })
        })


        table.on('tool(plugin-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            }
        })

        table.on('toolbar(plugin-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'refresh') {
                window.refresh()
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj)
            }
        })

        form.on('submit(plugin-query)', function (data) {
            window.refresh(data.field)
            return false
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            })
        }


        window.remove = function (obj) {
            layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['plugin_folder_name'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del()
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var ids = []
            var hasCheck = table.checkStatus('plugin-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            {#console.log(ids);#}
            layer.confirm('确定要删除选中数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({

                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('plugin-table')
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.refresh = function (param) {
            table.reload('plugin-table', {where: param})
        }
    })
</script>
</html>